﻿using Aker.DevTeam;
using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace Aker
{
    public partial class HammaddeReport : XtraReport
    {
        int EmptyLabelCount;
        public bool Stop = false;
        private int Tip;
        private string Ids;

        public HammaddeReport(int tip, string ids, int emptyCount, int copyCount)
        {
            InitializeComponent();

            this.Tip = tip;
            this.Ids = ids;

            this.PrintingSystem.ShowMarginsWarning = false;

            DataTable dtReport = Data.DM.ExecuteDataTable("GetHammaddeReport", Tip, Ids);
            dtReport.TableName = "T";

            dtReport.Columns.Add("PrintCount", typeof(int));

            DataSet ds = new DataSet();
            ds.Tables.Add(dtReport);

            EmptyLabelCount = emptyCount;

            DataTable dt = ds.Tables[0];
            DataTable dtOrj = dt.Copy();

            for (int i = 0; i < dtOrj.Rows.Count; i++)
            {
                DataRow drOrj = dtOrj.Rows[i];
                if (drOrj["PrintCount"] == DBNull.Value)
                    drOrj["PrintCount"] = 1;

                int count = (int)drOrj["PrintCount"] - 1;
                for (int j = 0; j < count; j++)
                    dt.ImportRow(drOrj);
            }

            DataView dv = dt.DefaultView;
            //dv.Sort = "Sira";
            DataTable sortedDT = dv.ToTable();

            ds.Tables.Remove("T");
            ds.Tables.Add(sortedDT);

            placeHolder.ForeColor = Color.White;

            DataSet dsCopy = ds.Copy();

            for (int i = 0; i < copyCount - 1; i++)
                ds.Merge(dsCopy);

            for (int i = 0; i < EmptyLabelCount; i++)
                sortedDT.Rows.InsertAt(sortedDT.NewRow(), 0);


            this.DataMember = "T";
            this.DataSource = ds;

            tbElyaf.BeforePrint += table_BeforePrint1;
            tbIplik.BeforePrint += table_BeforePrint2;

            tcEAciklama.BeforePrint += tc_BeforePrint;
            tcECekme.BeforePrint += tc_BeforePrint;
            tcECins.BeforePrint += tc_BeforePrint;
            tcEKalinlik.BeforePrint += tc_BeforePrint;
            tcEKesit.BeforePrint += tc_BeforePrint;
            tcEKullanim.BeforePrint += tc_BeforePrint;
            tcECuvalBot.BeforePrint += tc_BeforePrint;
            tcEParlaklik.BeforePrint += tc_BeforePrint;
            tcERenk.BeforePrint += tc_BeforePrint;
            tcETedarikci.BeforePrint += tc_BeforePrint;
            tcEKG.BeforePrint += tc_BeforePrint;

            tcIKalinlik.BeforePrint += tc_BeforePrint;
            tcICekim.BeforePrint += tc_BeforePrint;
            tcICekme.BeforePrint += tc_BeforePrint;
            tcIParlaklik.BeforePrint += tc_BeforePrint;
            tcIKarisim.BeforePrint += tc_BeforePrint;
            tcIOran.BeforePrint += tc_BeforePrint;
            tcIOzellik.BeforePrint += tc_BeforePrint;
            tcIRenk.BeforePrint += tc_BeforePrint;
            tcICuvalBot.BeforePrint += tc_BeforePrint;
            tcITedarikci.BeforePrint += tc_BeforePrint;
            tcIKG.BeforePrint += tc_BeforePrint;


            if (Tip == 1)
            {
                tbElyaf.Visible = true;
                tbElyaf.LocationF = tbIplik.LocationF;

                tcEAciklama.Text = "";
                tcECekme.Text = "";
                tcECins.Text = "";
                tcEKalinlik.Text = "";
                tcEKesit.Text = "";
                tcEKullanim.Text = "";
                tcECuvalBot.Text = "";
                tcELot.Text = "";
                tcEParlaklik.Text = "";
                tcERenk.Text = "";
                tcETedarikci.Text = "";

                tcEAciklama.DataBindings.Add("Text", null, "T.GrupAciklama");
                tcECekme.DataBindings.Add("Text", null, "T.Cekim");
                tcECins.DataBindings.Add("Text", null, "T.Cins");
                tcEKalinlik.DataBindings.Add("Text", null, "T.Kalinlik");
                tcEKesit.DataBindings.Add("Text", null, "T.Kesit");
                tcEKullanim.DataBindings.Add("Text", null, "T.Kullanim");
                tcELot.DataBindings.Add("Text", null, "T.Lot");
                tcEParlaklik.DataBindings.Add("Text", null, "T.Parlaklik");
                tcERenk.DataBindings.Add("Text", null, "T.ElyafRenk");
                tcETedarikci.DataBindings.Add("Text", null, "T.Tedarikci");
                tcEKG.DataBindings.Add("Text", null, "T.KG", "{0:n2}");
                tcECuvalBot.DataBindings.Add("Text", null, "T.CuvalBot", "{0:n0}");

                BarkodElyaf.DataBindings.Add("Text", null, "T.ElyafId");
                lblElyafId.DataBindings.Add("Text", null, "T.ElyafId");
            }

            else if (Tip == 2)
            {
                tbIplik.Visible = true;

                tcIKalinlik.Text = "";
                tcICekim.Text = "";
                tcICekme.Text = "";
                tcIParlaklik.Text = "";
                tcIKarisim.Text = "";
                tcIOran.Text = "";
                tcIOzellik.Text = "";
                tcIRenk.Text = "";
                tcICuvalBot.Text = "";
                tcILot.Text = "";
                tcITedarikci.Text = "";

                tcIKalinlik.DataBindings.Add("Text", null, "T.Kalinlik");
                tcICekim.DataBindings.Add("Text", null, "T.Cekim");
                tcICekme.DataBindings.Add("Text", null, "T.CekmeTipi");
                tcIParlaklik.DataBindings.Add("Text", null, "T.Parlaklik");
                tcIKarisim.DataBindings.Add("Text", null, "T.Karisim");
                tcIOran.DataBindings.Add("Text", null, "T.Oran");
                tcIOzellik.DataBindings.Add("Text", null, "T.Fantazi");
                tcIRenk.DataBindings.Add("Text", null, "T.IplikRenk");
                tcILot.DataBindings.Add("Text", null, "T.Lot");
                tcITedarikci.DataBindings.Add("Text", null, "T.Tedarikci");
                tcIKG.DataBindings.Add("Text", null, "T.KG", "{0:n2}");
                tcICuvalBot.DataBindings.Add("Text", null, "T.CuvalBot", "{0:n0}");

                BarkodIp.DataBindings.Add("Text", null, "T.IplikId");
                lblIpId.DataBindings.Add("Text", null, "T.IplikId");
            }

            Detail.HeightF = 500;
        }

        private void Detail_BeforePrint(object sender, PrintEventArgs e)
        {
            EmptyLabelCount--;
        }

        private void tc_BeforePrint(object sender, PrintEventArgs e)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 35f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;

            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }

        private void table_BeforePrint1(object sender, PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
                e.Cancel = true;
        }

        private void table_BeforePrint2(object sender, PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
                e.Cancel = true;
        }

    }
}
